package gui;

import javax.swing.JPanel;

import Entities.UserEntity;
import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JProgressBar;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.jar.Attributes.Name;

import javax.swing.JTextField;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
/**
 * 
 * @author Idan Mann
 * This GUI class is presenting the Information Department Manager with the option to manage and change
 * employees' roles in the system.
 *
 */
public class ManageEmployeesPanel extends JPanel implements ChatIF
{
	private AcademicFrame frame;
	private JPanel previousPanel;
	private boolean isIDM;
	private JRadioButton rdbtnExisting;
	private JRadioButton rdbtnAdd;
	private JComboBox comboBox;
	private JLabel lblName;
	private JLabel lblUserId;
	private JTextField userIDtextField;
	private JButton btnChoose;
	private JCheckBox chckbxSupervisor;
	private JCheckBox chckbxCommittee;
	private JCheckBox chckbxCommitteeChairman;
	private JCheckBox chckbxAssessor;
	private JCheckBox chckbxExecutionLeader;
	private JButton btnUpdate;
	private JButton btnClose;
	private ArrayList<Object> listOfEmployees;
	private int amountOfEmployees;
	private JLabel label;
	private JTextField firstname;
	private JLabel label_1;
	private JTextField email;
	private JTextField department;
	private JLabel label_2;
	private JTextField phone;
	private JLabel label_3;
	private JTextField title;
	private JLabel label_4;
	private JLabel label_5;
	private JTextField lastname;
	private JLabel lblSystemAccess;
	private JLabel lblUserInformation;
	private ArrayList<UserEntity> committeeUID;
	private UserEntity chairman;
	private UserEntity currentUser;
	private String userID;
	private JButton btnClear;
	private UserEntity employee;
	private boolean committeeUnselect = false;
	private boolean chairmanUnselect= false;
	
	public ManageEmployeesPanel (AcademicFrame frame, JPanel previousPanel)
	{
		this.frame = frame;
		this.previousPanel = previousPanel;
		
		committeeUID = new ArrayList<UserEntity>();
		
		initialize();
	}
	private void initialize()
	{
		setLayout(null);
		
		JLabel lblManageEmployees = new JLabel("Manage Employees");
		lblManageEmployees.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblManageEmployees.setBounds(243, 30, 184, 20);
		add(lblManageEmployees);
		
		lblName = new JLabel("Name");
		lblName.setBounds(211, 123, 54, 14);
		add(lblName);
		
		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				int index = comboBox.getSelectedIndex();
				
				if (comboBox.getSelectedItem().toString().equalsIgnoreCase("--Choose an employee--"))
				{
					firstname.setText("");
					lastname.setText("");
					email.setText("");
					phone.setText("");
					title.setText("");
					department.setText("");
					
					chckbxAssessor.setSelected(false);
					chckbxCommittee.setSelected(false);
					chckbxCommitteeChairman.setSelected(false);
					chckbxExecutionLeader.setSelected(false);
					chckbxSupervisor.setSelected(false);
					
					chckbxAssessor.setEnabled(false);
					chckbxCommittee.setEnabled(false);
					chckbxCommitteeChairman.setEnabled(false);
					chckbxExecutionLeader.setEnabled(false);
					chckbxSupervisor.setEnabled(false);
					
				}
				else
				{
					currentUser = (UserEntity)listOfEmployees.get(index+1);
					userID = currentUser.getUserID();
					
					// Set information
					firstname.setText(currentUser.getFirstName());
					lastname.setText(currentUser.getLastName());
					email.setText(currentUser.getEmail());
					phone.setText(currentUser.getPhone());
					title.setText(currentUser.getTitle());
					department.setText(currentUser.getDepartment());
					
					setAccess(currentUser);
					
					chckbxAssessor.setEnabled(true);
					chckbxCommittee.setEnabled(true);
					chckbxExecutionLeader.setEnabled(true);
					chckbxSupervisor.setEnabled(true);				
					
				}
			}

		
		});

		comboBox.setBounds(275, 119, 132, 22);
		add(comboBox);
		
		chckbxSupervisor = new JCheckBox("Supervisor");
		chckbxSupervisor.setBounds(170, 288, 97, 23);
		add(chckbxSupervisor);
		
		chckbxCommittee = new JCheckBox("Changes Committee Member");
		chckbxCommittee.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				if (!chckbxCommittee.isSelected())
				{
				
					committeeUnselect = true;
				}
				else
				{
					committeeUnselect = false;
				}
			}
		});
		chckbxCommittee.addItemListener(new ItemListener() 
		{
			public void itemStateChanged(ItemEvent arg0) 
			{
				if (chckbxCommittee.isSelected())
				{
					chckbxCommitteeChairman.setEnabled(true);
				}
				else
				{
					chckbxCommitteeChairman.setEnabled(false);
					chckbxCommitteeChairman.setSelected(false);
				}
			}
		});
		chckbxCommittee.setBounds(170, 339, 176, 23);
		add(chckbxCommittee);
		
		chckbxAssessor = new JCheckBox("Assessor");
		chckbxAssessor.setBounds(419, 288, 97, 23);
		add(chckbxAssessor);
		
		chckbxExecutionLeader = new JCheckBox("Execution Leader");
		chckbxExecutionLeader.setBounds(419, 339, 176, 23);
		add(chckbxExecutionLeader);
		
		chckbxCommitteeChairman = new JCheckBox("Committee Chairman");
		chckbxCommitteeChairman.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (!chckbxCommitteeChairman.isSelected())
				{
					
					chairmanUnselect = true;
				}
				else
				{
					 
					chairmanUnselect = false;
				}
				
			}
		});
		chckbxCommitteeChairman.setEnabled(false);
		chckbxCommitteeChairman.setBounds(210, 365, 136, 23);
		add(chckbxCommitteeChairman);
		
		rdbtnExisting = new JRadioButton("Existing employees");
		rdbtnExisting.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				rdbtnExisting.setSelected(true);
				rdbtnAdd.setSelected(false);
				comboBox.setSelectedIndex(0);
			}
		});
		rdbtnExisting.addChangeListener(new ChangeListener() 
		{
			public void stateChanged(ChangeEvent arg0) 
			{
				if (rdbtnExisting.isSelected())
				{
					
				}
				else
				{
					
					
				}
			}
		});
		
				
				
			
	
		rdbtnExisting.setSelected(true);
		rdbtnExisting.setBounds(150, 72, 136, 23);
		add(rdbtnExisting);
		
		rdbtnAdd = new JRadioButton("Add employees");
		rdbtnAdd.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				//userIDtextField.setText("");
				rdbtnAdd.setSelected(true);
				rdbtnExisting.setSelected(false);
			}
		});
		rdbtnAdd.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e) 
			{
				//userIDtextField.setText(userIDtextField.getText());
				if (rdbtnAdd.isSelected())
				{
					btnChoose.setVisible(true);
					userIDtextField.setVisible(true);
					lblUserId.setVisible(true);
					btnClear.setVisible(true);
					
					lblName.setVisible(false);
					comboBox.setVisible(false);
					
					// reset fields
					//userIDtextField.setText("");
					clearTextFields();
					
					chckbxAssessor.setSelected(false);
					chckbxCommittee.setSelected(false);
					chckbxCommitteeChairman.setSelected(false);
					chckbxExecutionLeader.setSelected(false);
					chckbxSupervisor.setSelected(false);
					
					chckbxAssessor.setEnabled(false);
					chckbxCommittee.setEnabled(false);
					chckbxCommitteeChairman.setEnabled(false);
					chckbxExecutionLeader.setEnabled(false);
					chckbxSupervisor.setEnabled(false);
					
				}
				else
				{
					btnChoose.setVisible(false);
					userIDtextField.setVisible(false);
					lblUserId.setVisible(false);
					btnClear.setVisible(false);
					
					lblName.setVisible(true);
					comboBox.setVisible(true);
					
				}
			}
		});
	
		
		rdbtnAdd.setBounds(402, 72, 157, 23);
		add(rdbtnAdd);
		
		btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(new ActionListener()
		{
			

			public void actionPerformed(ActionEvent e) 
			{
				
				currentUser.setFirstName(firstname.getText());
				currentUser.setLastName(lastname.getText());
				if(chckbxAssessor.isSelected())
					currentUser.setIsAssessor("1");
				else
					currentUser.setIsAssessor("0");
				if(chckbxExecutionLeader.isSelected())
					currentUser.setIsExecutionLeader("1");
				else
					currentUser.setIsExecutionLeader("0");
				if(chckbxSupervisor.isSelected())
					currentUser.setIsSupervisor("1");
				else
					currentUser.setIsSupervisor("0");
				if(chckbxCommittee.isSelected())
					currentUser.setIsCommitteeMember("1");
				else
					currentUser.setIsCommitteeMember("0");
				if(chckbxCommitteeChairman.isSelected())
					currentUser.setIsChairman("1");
				else
					currentUser.setIsChairman("0");
					
				if (rdbtnAdd.isSelected()) // new employee
				{	
					if (userIDtextField.getText().isEmpty())
						(new Dialog("Please enter an employee ID first.")).setVisible(true);
					else
					{
						currentUser.setUserID(userIDtextField.getText());
						
						if (chckbxCommittee.isSelected())
						{
							if (chckbxCommitteeChairman.isSelected())
							{
								if (userIDtextField.getText().equalsIgnoreCase(chairman.getUserID()))
								{
									update(currentUser);
								}
								else
								{
									switchChairman();
								}
							}
							else
							{
								if(chairmanUnselect)
								{
									(new Dialog ("Can only replace the chairman with another one.")).setVisible(true);
									//chckbxCommitteeChairman.setSelected(true);
									clearTextFields();
									
								}
								else
								{
								
								
									boolean existingCommitteeMember=false;
									for (int i=0; i<3 ; i++)
									{
										if (userIDtextField.getText().equalsIgnoreCase(  (committeeUID.get(i)).getUserID() ) )
										{
											existingCommitteeMember=true;
											break;
										}
									}
									
									if (existingCommitteeMember)
									{
										update(currentUser);
									}
									else
									{
										switchCommittee();
									}
								
								}
								
							}
						}
						else
						{
							if (committeeUnselect)
							{
								
								(new Dialog ("Can only replace a committee member with another one.")).setVisible(true);
							//	chckbxCommittee.setSelected(true);
								clearTextFields();
							}
							else	
							{
								update(currentUser);
							}
						}
					}
				}
				else // Add existing employee
				{
					if (comboBox.getSelectedIndex()==0)
						(new Dialog("Please select an employee from the list first.")).setVisible(true);
					else
					{
						employee = (UserEntity) listOfEmployees.get(comboBox.getSelectedIndex() + 1);
						
						if (chckbxCommittee.isSelected())
						{
							if (chckbxCommitteeChairman.isSelected())
							{
								
								
								if ( employee.getUserID().equalsIgnoreCase(chairman.getUserID()))
								{
									update(currentUser);
								}
								else
								{
									switchChairman();
								}
							}
							else
							{
								if(chairmanUnselect)
								{
									(new Dialog ("Can only replace the chairman with another one.")).setVisible(true);
									//chckbxCommitteeChairman.setSelected(true);
									clearTextFields();
								}
								else
								{
									boolean existingCommitteeMember=false;
									for (int i=0; i<3 ; i++)
									{
										if (employee.getUserID().equalsIgnoreCase( (committeeUID.get(i)).getUserID() ))
										{
											existingCommitteeMember=true;
											break;
										}
									}
									
									if (existingCommitteeMember)
									{
										update(currentUser);
									}
									else
									{
										switchCommittee();
									}
								}
								
								
								
							}
						}
						else
						{
							if (committeeUnselect)
							{
								(new Dialog ("Can only replace a committee member with another one.")).setVisible(true);
								//chckbxCommittee.setSelected(true);
								clearTextFields();
							}
							else
							{
								update(currentUser);
							}
						}
					}
					
				}
				
			}

		
		});
		btnUpdate.setBounds(377, 431, 91, 23);
		add(btnUpdate);
		
		lblUserId = new JLabel("User ID");
		lblUserId.setBounds(211, 123, 46, 14);
		add(lblUserId);
		
		userIDtextField = new JTextField();
		userIDtextField.setBounds(275, 120, 113, 20);
		add(userIDtextField);
		userIDtextField.setColumns(10);
		
		btnChoose = new JButton("Choose");
		btnChoose.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e)
			{
				if (userIDtextField.getText().isEmpty())
				{
					(new Dialog ("Please enter user ID first.")).setVisible(true);
				}
				else
				{
					btnChoose.setEnabled(false);
					userIDtextField.setEnabled(false);
					userID = userIDtextField.getText();
					ArrayList<Object> getUserInfo = new ArrayList<Object>();
					getUserInfo.add("getInfo");
					getUserInfo.add(userID);
					AcademicFrame.myclient.sendMessageToServer(getUserInfo);
				}
				
				
				
			}
		});
		btnChoose.setBounds(417, 119, 91, 23);
		add(btnChoose);
		
		btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				frame.setContentPane(previousPanel);
				frame.setSize(500, 400);
			}
		});
		btnClose.setBounds(197, 431, 91, 23);
		add(btnClose);
		
		label = new JLabel("First name");
		label.setBounds(99, 178, 73, 14);
		add(label);
		
		firstname = new JTextField();
		firstname.setEditable(false);
		firstname.setColumns(10);
		firstname.setBounds(171, 175, 141, 20);
		add(firstname);
		
		label_1 = new JLabel("E-mail");
		label_1.setBounds(346, 178, 73, 14);
		add(label_1);
		
		email = new JTextField();
		email.setEditable(false);
		email.setColumns(10);
		email.setBounds(418, 175, 141, 20);
		add(email);
		
		department = new JTextField();
		department.setEditable(false);
		department.setColumns(10);
		department.setBounds(418, 208, 141, 20);
		add(department);
		
		label_2 = new JLabel("Department");
		label_2.setBounds(346, 211, 73, 14);
		add(label_2);
		
		phone = new JTextField();
		phone.setEditable(false);
		phone.setColumns(10);
		phone.setBounds(418, 242, 141, 20);
		add(phone);
		
		label_3 = new JLabel("Phone");
		label_3.setBounds(346, 245, 73, 14);
		add(label_3);
		
		title = new JTextField();
		title.setEditable(false);
		title.setColumns(10);
		title.setBounds(171, 242, 141, 20);
		add(title);
		
		label_4 = new JLabel("Title");
		label_4.setBounds(99, 245, 73, 14);
		add(label_4);
		
		label_5 = new JLabel("Last name");
		label_5.setBounds(99, 211, 73, 14);
		add(label_5);
		
		lastname = new JTextField();
		lastname.setEditable(false);
		lastname.setColumns(10);
		lastname.setBounds(171, 208, 141, 20);
		add(lastname);
		
		lblSystemAccess = new JLabel("System Access");
		lblSystemAccess.setBounds(61, 292, 103, 14);
		add(lblSystemAccess);
		
		lblUserInformation = new JLabel("User Details");
		lblUserInformation.setBounds(61, 153, 111, 14);
		add(lblUserInformation);
		
		btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e)
			{
				clearTextFields();
				userIDtextField.setEnabled(true);
				
			}
		});
		btnClear.setBounds(523, 119, 91, 23);
		add(btnClear);
		
		lblUserId.setVisible(false);
		userIDtextField.setVisible(false);
		btnChoose.setVisible(false);
		btnClear.setVisible(false);
		
		
		rdbtnAdd.setRolloverEnabled(false);
		rdbtnExisting.setRolloverEnabled(false);
	}
	/**
	 * 
	 * this method clears all the text fields and resets user made selections
	 */
	private void clearTextFields()
	{
		firstname.setText("");
		lastname.setText("");
		email.setText("");
		phone.setText("");
		title.setText("");
		department.setText("");
		userIDtextField.setText("");
		comboBox.setSelectedIndex(0);
		btnChoose.setEnabled(true);
		
		UserEntity dummyUser = new UserEntity("","");
		setAccess(dummyUser);
		
	}
	/**
	 * 
	 * @param currentUser - User Entity of the current selected user
	 * This method sets the GUI to represent the current user's access level in the system
	 */
	private void setAccess(UserEntity currentUser)
	{
		// TODO Auto-generated method stub// Set access
		if (currentUser.getIsAssessor().equalsIgnoreCase("1"))
			chckbxAssessor.setSelected(true);
		else
			chckbxAssessor.setSelected(false);
		if (currentUser.getIsCommitteeMember().equalsIgnoreCase("1"))
		{
			chckbxCommittee.setSelected(true);
			if (currentUser.getIsChairman().equalsIgnoreCase("1"))
				chckbxCommitteeChairman.setSelected(true);
			else
				chckbxCommitteeChairman.setSelected(false);
		}
		else
		{
			chckbxCommittee.setSelected(false);
			chckbxCommitteeChairman.setSelected(false);
		}
		if (currentUser.getIsExecutionLeader().equalsIgnoreCase("1"))
			chckbxExecutionLeader.setSelected(true);
		else
			chckbxExecutionLeader.setSelected(false);
		if (currentUser.getIsSupervisor().equalsIgnoreCase("1"))
			chckbxSupervisor.setSelected(true);
		else
			chckbxSupervisor.setSelected(false);
		
	}

	/**
	 * This method changes to the switch committee member window
	 */
	private void switchCommittee()
	{
		
		frame.setContentPane(new SwitchCommitteeMemberPanel(frame, this, committeeUID, currentUser));
		frame.setSize(520 ,290);
		
	}
	/**
	 * This method changes to the switch chairman window
	 */
	private void switchChairman()
	{

		frame.setContentPane(new SwitchChairmanPanel(frame, this, chairman, currentUser ));
		frame.setSize(420, 256);
		
	}
	/**
	 * This method sends the server a request to set the user with the desired access level
	 * @param userToSet
	 */
	private void update(UserEntity userToSet)
	{
		ArrayList<Object> setUserAccess = new ArrayList<Object>();
		setUserAccess.add("setUserAccess");
		setUserAccess.add(userToSet);
		AcademicFrame.myclient.sendMessageToServer(setUserAccess);
	}
	@Override
	public void display(Object message) 
	{
		ArrayList<Object> answer = new ArrayList<Object>();
		answer = (ArrayList<Object>)message;
		
		if (	((String)(answer.get(0))).equalsIgnoreCase("initializeMangeEmployees")	)
		{
			
			if (	((String)(answer.get(1))).equalsIgnoreCase("0")	)
			{
				(new Dialog("Error: No employees found.")).setVisible(true);
				frame.setContentPane(previousPanel);
				frame.setSize(500,400);
			}
			else
			{
				
				this.amountOfEmployees = Integer.parseInt((String)(answer.get(1)));	
				this.listOfEmployees = answer;
				comboBox.addItem("--Choose an employee--");
				for (int i=0; i<amountOfEmployees; i++)
				{
					employee = (UserEntity)(listOfEmployees.get(2+i));
				
					//comboBox.addItem( ( (UserEntity)(answer.get(2+i))).getFirstName() + " " + ( (UserEntity)(answer.get(2+i))).getLastName() );
					comboBox.addItem( employee.getFirstName() + " " + employee.getLastName() );
					
					
					if ( employee.getIsChairman().equalsIgnoreCase("1")	)
					{
						
						this.chairman = employee;
					}
					if ( employee.getIsCommitteeMember().equalsIgnoreCase("1")	)
					{					
						this.committeeUID.add(employee); 
					}
					
				
				}
				
			}
		}
		else
			if (	((String)(answer.get(0))).equalsIgnoreCase("userInfo")	)
			{
				if (	((String)(answer.get(1))).equalsIgnoreCase("No User Error.")	)
				{
					(new Dialog ("No such user ID in the database")).setVisible(true);
					userIDtextField.setText("");
				}
				else
				{
					boolean itsAnAmployee = false;
					firstname.setText((String) answer.get(1));
					lastname.setText((String) answer.get(2));
					email.setText((String) answer.get(3));
					title.setText((String) answer.get(4));
					department.setText((String) answer.get(5));
					phone.setText((String) answer.get(6));
					
					chckbxAssessor.setEnabled(true);
					chckbxCommittee.setEnabled(true);
					chckbxExecutionLeader.setEnabled(true);
					chckbxSupervisor.setEnabled(true);	
					
					for (int j=0; j<amountOfEmployees; j++)
					{
						currentUser = (UserEntity)listOfEmployees.get(j+2);

						if (userID.equalsIgnoreCase(currentUser.getUserID()))
						{
							setAccess(currentUser);
							itsAnAmployee=true;
							break;
						}
					}
					
					if (!itsAnAmployee)
					{
						chckbxAssessor.setSelected(false);
						chckbxCommittee.setSelected(false);
						chckbxExecutionLeader.setSelected(false);
						chckbxSupervisor.setSelected(false);
					}
				}
			}
			else
				if (	((String)(answer.get(0))).equalsIgnoreCase("userAccessWasSet")	)
				{

					if (	((String)(answer.get(1))).equalsIgnoreCase("Valid")	)
					{
						(new Dialog("User access level was updated.")).setVisible(true);
						
						frame.setContentPane(new ManageEmployeesPanel(frame,previousPanel));
						frame.setSize(651,501);
						frame.setSize(650,500);
						
						ArrayList<Object> getListOfEmployees = new ArrayList<Object>();
						getListOfEmployees.add("getListOfEmployees");
						AcademicFrame.myclient.sendMessageToServer(getListOfEmployees);
						
					
					}
					else
					{
						(new Dialog((String)(answer.get(1)))).setVisible(true);
		
					}
				}
		
	}
}
;